#include "mqtt.h"
#include "usart.h"
#include <string.h>

mqtt_struct mqtt;

/**
  * @func	mqtt_connect_package
  * @param	none
  * @return	none
  */
void mqtt_connect_package(void)
{
	uint16_t i;
	
	mqtt.message_id = 1;
	mqtt.fixed_header_len = 1;
	mqtt.variable_header_len = 10;
	mqtt.payload_len = 2 + strlen(MQTT_CLIENTID) + 2 + strlen(MQTT_USERNAME) + 2 + strlen(MQTT_PASSWORD);
	mqtt.remaining_len = mqtt.variable_header_len + mqtt.payload_len;
	
	/* mqtt固定头 */
	mqtt.pack_buff[0] = 0x10;				
	do{
		if(mqtt.remaining_len / 128 == 0)
			mqtt.pack_buff[mqtt.fixed_header_len] = mqtt.remaining_len;
		else
			mqtt.pack_buff[mqtt.fixed_header_len] = (mqtt.remaining_len % 128) | 0x80;
		
		mqtt.fixed_header_len++;
		mqtt.remaining_len = mqtt.remaining_len / 128;
	}while(mqtt.remaining_len);
	
	/* mqtt可变头 */
	mqtt.pack_buff[mqtt.fixed_header_len + 0] = 0x00;
	mqtt.pack_buff[mqtt.fixed_header_len + 1] = 0x04;
	mqtt.pack_buff[mqtt.fixed_header_len + 2] = 0x4d;		/* 'M' */
	mqtt.pack_buff[mqtt.fixed_header_len + 3] = 0x51;		/* 'Q' */
	mqtt.pack_buff[mqtt.fixed_header_len + 4] = 0x54;		/* 'T' */
	mqtt.pack_buff[mqtt.fixed_header_len + 5] = 0x54;		/* 'T' */
	mqtt.pack_buff[mqtt.fixed_header_len + 6] = 0x04;
	mqtt.pack_buff[mqtt.fixed_header_len + 7] = 0xc2;
	mqtt.pack_buff[mqtt.fixed_header_len + 8] = 0x00;
	mqtt.pack_buff[mqtt.fixed_header_len + 9] = 0x64;
	
	/* mqtt有效载荷 */
	mqtt.pack_buff[mqtt.fixed_header_len + 10] = (uint8_t)(strlen(MQTT_CLIENTID) >> 8);
	mqtt.pack_buff[mqtt.fixed_header_len + 11] = (uint8_t)strlen(MQTT_CLIENTID);
	memcpy(&mqtt.pack_buff[mqtt.fixed_header_len + 12], MQTT_CLIENTID, strlen(MQTT_CLIENTID));
	
	mqtt.pack_buff[mqtt.fixed_header_len + 12 + strlen(MQTT_CLIENTID)] = (uint8_t)(strlen(MQTT_USERNAME) >> 8);
	mqtt.pack_buff[mqtt.fixed_header_len + 13 + strlen(MQTT_CLIENTID)] = (uint8_t)strlen(MQTT_USERNAME);
	memcpy(&mqtt.pack_buff[mqtt.fixed_header_len + 14 + strlen(MQTT_CLIENTID)], MQTT_USERNAME, strlen(MQTT_USERNAME));
	
	mqtt.pack_buff[mqtt.fixed_header_len + 14 + strlen(MQTT_CLIENTID) + strlen(MQTT_USERNAME)] = (uint8_t)(strlen(MQTT_PASSWORD) >> 8);
	mqtt.pack_buff[mqtt.fixed_header_len + 15 + strlen(MQTT_CLIENTID) + strlen(MQTT_USERNAME)] = (uint8_t)strlen(MQTT_PASSWORD);
	memcpy(&mqtt.pack_buff[mqtt.fixed_header_len + 16 + strlen(MQTT_CLIENTID) + strlen(MQTT_USERNAME)], MQTT_PASSWORD, strlen(MQTT_PASSWORD));
	
	for(i = 0; i < (mqtt.fixed_header_len + mqtt.variable_header_len + mqtt.payload_len); i++)
	{
		usart0_printf("%02x ", mqtt.pack_buff[i]);
	}
}

/**
  * @func	mqtt_connect_package
  * @param	none
  * @return	none
  */
void mqtt_publish(char *topic, char *data)
{
	uint16_t i;
	
	mqtt.fixed_header_len = 1;
	mqtt.variable_header_len = 2 + strlen(topic);
	mqtt.payload_len = strlen(data);
	mqtt.remaining_len = mqtt.variable_header_len = mqtt.payload_len;
	
	/* mqtt固定头 */
	mqtt.pack_buff[0] = 0x30;
	do{
		if(mqtt.remaining_len / 128 == 0)
			mqtt.pack_buff[mqtt.fixed_header_len] = mqtt.remaining_len;
		else
			mqtt.pack_buff[mqtt.fixed_header_len] = (mqtt.remaining_len % 128) | 0x80;
		
		mqtt.fixed_header_len++;
		mqtt.remaining_len = mqtt.remaining_len / 128;
	}while(mqtt.remaining_len);

	/* mqtt有效载荷 */
	mqtt.pack_buff[mqtt.fixed_header_len + 0] = (uint8_t)(strlen(topic) << 8);
	mqtt.pack_buff[mqtt.fixed_header_len + 1] = (uint8_t)strlen(topic);
	memcpy(&mqtt.pack_buff[mqtt.fixed_header_len + 2], topic, strlen(topic));
	memcpy(&mqtt.pack_buff[mqtt.fixed_header_len + 2 + strlen(topic)], data, strlen(data));
	
	for(i = 0; i < (mqtt.fixed_header_len + mqtt.variable_header_len + mqtt.payload_len); i++)
	{
		usart0_printf("%02x ", mqtt.pack_buff[i]);
	}
}

